package com.ruoyi.project.upload.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/upload/file")
public class UploadController extends BaseController {
    //声明公告地址
    String prfix="/upload/file";
    //声明单元方法：转发上传页面
    @RequiresPermissions("upload:file:view")
    @GetMapping
    public String getPage(){
        return prfix+"/file";
    }
    //声明单元方法：处理资源上传
    @RequiresPermissions("upload:file:uploadFile")
    @RequestMapping("/uploadFile")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file_data) throws IOException {
        //1.获取文件后缀名
        String oldName = file_data.getOriginalFilename();
        String suffix=oldName.substring(oldName.lastIndexOf("."));
        //2.创建文件存储名
        String newName= UUID.randomUUID()+""+System.currentTimeMillis()+suffix;
        //3.完成文件存储
        file_data.transferTo(new File("D:/img",newName));
        //5.响应结果

        return new AjaxResult(AjaxResult.Type.SUCCESS,"成功",newName);
    }
}
